package com.example.socialapp.ui.stats

import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.DateRange
import androidx.compose.material.icons.filled.Share
import androidx.compose.material.icons.outlined.Comment
import androidx.compose.material.icons.outlined.Favorite
import androidx.compose.material.icons.outlined.Forum
import androidx.compose.material.icons.outlined.Group
import androidx.compose.material.icons.outlined.PostAdd
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Tab
import androidx.compose.material3.TabRow
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.PathEffect
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import kotlin.math.max
import kotlin.random.Random

/**
 * 用户活跃度统计界面
 *
 * @param userId 用户ID
 * @param onNavigateBack 返回上一界面回调
 * @param onShare 分享统计数据回调
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun UserStatsScreen(
    userId: String = "",
    onNavigateBack: () -> Unit,
    onShare: () -> Unit = {}
) {
    val scrollState = rememberScrollState()
    
    // 在实际应用中应从ViewModel获取用户统计数据
    val userStats = remember { generateSampleUserStats() }
    var selectedTimeRange by remember { mutableStateOf(TimeRange.WEEK) }
    
    Scaffold(
        topBar = {
            TopAppBar(
                title = { Text("活跃度统计") },
                navigationIcon = {
                    IconButton(onClick = onNavigateBack) {
                        Icon(
                            imageVector = Icons.Default.ArrowBack,
                            contentDescription = "返回"
                        )
                    }
                },
                actions = {
                    IconButton(onClick = onShare) {
                        Icon(
                            imageVector = Icons.Default.Share,
                            contentDescription = "分享"
                        )
                    }
                },
                colors = TopAppBarDefaults.topAppBarColors(
                    containerColor = MaterialTheme.colorScheme.primaryContainer,
                    titleContentColor = MaterialTheme.colorScheme.onPrimaryContainer
                )
            )
        }
    ) { innerPadding ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(innerPadding)
                .verticalScroll(scrollState)
        ) {
            // 时间范围选择器
            TimeRangeSelector(
                selectedRange = selectedTimeRange,
                onRangeSelected = { selectedTimeRange = it }
            )
            
            // 活动概览卡片
            ActivityOverviewCard(userStats)
            
            // 参与度趋势图表
            EngagementTrendCard(userStats, selectedTimeRange)
            
            // 内容表现卡片
            ContentPerformanceCard(userStats)
            
            // 社交互动卡片
            SocialInteractionCard(userStats)
            
            Spacer(modifier = Modifier.height(24.dp))
        }
    }
}

/**
 * 时间范围选择器
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun TimeRangeSelector(
    selectedRange: TimeRange,
    onRangeSelected: (TimeRange) -> Unit
) {
    Card(
        modifier = Modifier
            .fillMaxWidth()
            .padding(16.dp),
        shape = RoundedCornerShape(12.dp),
        colors = CardDefaults.cardColors(
            containerColor = MaterialTheme.colorScheme.surfaceVariant
        )
    ) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Icon(
                imageVector = Icons.Default.DateRange,
                contentDescription = null,
                tint = MaterialTheme.colorScheme.primary
            )
            
            TabRow(
                selectedTabIndex = selectedRange.ordinal,
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(start = 8.dp)
            ) {
                TimeRange.values().forEach { range ->
                    Tab(
                        text = { Text(range.displayName) },
                        selected = selectedRange == range,
                        onClick = { onRangeSelected(range) }
                    )
                }
            }
        }
    }
}

/**
 * 活动概览卡片
 */
@Composable
private fun ActivityOverviewCard(userStats: UserStats) {
    Card(
        modifier = Modifier
            .fillMaxWidth()
            .padding(16.dp),
        shape = RoundedCornerShape(16.dp),
        elevation = CardDefaults.cardElevation(defaultElevation = 2.dp),
        colors = CardDefaults.cardColors(
            containerColor = MaterialTheme.colorScheme.surface
        )
    ) {
        Column(
            modifier = Modifier.padding(16.dp)
        ) {
            Text(
                text = "活动概览",
                style = MaterialTheme.typography.titleMedium,
                fontWeight = FontWeight.Bold,
                color = MaterialTheme.colorScheme.primary
            )
            
            Spacer(modifier = Modifier.height(16.dp))
            
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceEvenly
            ) {
                StatsItem(
                    icon = Icons.Outlined.PostAdd,
                    value = userStats.totalPosts.toString(),
                    label = "发布"
                )
                
                StatsItem(
                    icon = Icons.Outlined.Comment,
                    value = userStats.totalComments.toString(),
                    label = "评论"
                )
                
                StatsItem(
                    icon = Icons.Outlined.Favorite,
                    value = userStats.likesReceived.toString(),
                    label = "获赞"
                )
                
                StatsItem(
                    icon = Icons.Outlined.Group,
                    value = userStats.totalFollowers.toString(),
                    label = "粉丝"
                )
            }
            
            Spacer(modifier = Modifier.height(16.dp))
            
            Row(
                modifier = Modifier.fillMaxWidth(),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Box(
                    modifier = Modifier
                        .weight(userStats.activeLevel.toFloat())
                        .height(8.dp)
                        .clip(RoundedCornerShape(4.dp))
                        .background(MaterialTheme.colorScheme.primary)
                )
                
                Box(
                    modifier = Modifier
                        .weight(10 - userStats.activeLevel.toFloat())
                        .height(8.dp)
                        .clip(RoundedCornerShape(4.dp))
                        .background(MaterialTheme.colorScheme.outlineVariant.copy(alpha = 0.3f))
                )
            }
            
            Spacer(modifier = Modifier.height(8.dp))
            
            Text(
                text = "活跃等级: ${userStats.activeLevel}/10",
                style = MaterialTheme.typography.bodyMedium,
                color = MaterialTheme.colorScheme.primary,
                textAlign = TextAlign.Center,
                modifier = Modifier.fillMaxWidth()
            )
        }
    }
}

/**
 * 参与度趋势图表
 */
@Composable
private fun EngagementTrendCard(userStats: UserStats, timeRange: TimeRange) {
    Card(
        modifier = Modifier
            .fillMaxWidth()
            .padding(16.dp),
        shape = RoundedCornerShape(16.dp),
        elevation = CardDefaults.cardElevation(defaultElevation = 2.dp),
        colors = CardDefaults.cardColors(
            containerColor = MaterialTheme.colorScheme.surface
        )
    ) {
        Column(
            modifier = Modifier.padding(16.dp)
        ) {
            Text(
                text = "参与度趋势",
                style = MaterialTheme.typography.titleMedium,
                fontWeight = FontWeight.Bold,
                color = MaterialTheme.colorScheme.primary
            )
            
            Spacer(modifier = Modifier.height(16.dp))
            
            // 根据选择的时间范围获取不同的活动数据
            val activityData = when(timeRange) {
                TimeRange.WEEK -> userStats.weeklyActivity
                TimeRange.MONTH -> userStats.monthlyActivity
                TimeRange.YEAR -> userStats.yearlyActivity
            }
            
            ActivityTrendGraph(activityData)
            
            Spacer(modifier = Modifier.height(16.dp))
            
            // 图表图例
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceEvenly
            ) {
                ChartLegendItem(
                    color = MaterialTheme.colorScheme.primary,
                    label = "发布动态"
                )
                
                ChartLegendItem(
                    color = MaterialTheme.colorScheme.tertiary,
                    label = "评论互动"
                )
                
                ChartLegendItem(
                    color = MaterialTheme.colorScheme.secondary,
                    label = "点赞活动"
                )
            }
        }
    }
}

/**
 * 内容表现卡片
 */
@Composable
private fun ContentPerformanceCard(userStats: UserStats) {
    Card(
        modifier = Modifier
            .fillMaxWidth()
            .padding(16.dp),
        shape = RoundedCornerShape(16.dp),
        elevation = CardDefaults.cardElevation(defaultElevation = 2.dp),
        colors = CardDefaults.cardColors(
            containerColor = MaterialTheme.colorScheme.surface
        )
    ) {
        Column(
            modifier = Modifier.padding(16.dp)
        ) {
            Text(
                text = "内容表现",
                style = MaterialTheme.typography.titleMedium,
                fontWeight = FontWeight.Bold,
                color = MaterialTheme.colorScheme.primary
            )
            
            Spacer(modifier = Modifier.height(16.dp))
            
            TopPerformingPostItem(
                title = "最受欢迎的动态",
                content = userStats.topPost.content,
                stats = "${userStats.topPost.likes}赞 • ${userStats.topPost.comments}评论"
            )
            
            Spacer(modifier = Modifier.height(12.dp))
            
            TopPerformingPostItem(
                title = "互动最多的动态",
                content = userStats.mostEngagedPost.content,
                stats = "${userStats.mostEngagedPost.comments}评论 • ${userStats.mostEngagedPost.likes}赞"
            )
            
            Spacer(modifier = Modifier.height(12.dp))
            
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceEvenly
            ) {
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {
                    Text(
                        text = "${userStats.avgLikesPerPost}",
                        style = MaterialTheme.typography.titleLarge,
                        color = MaterialTheme.colorScheme.primary
                    )
                    Text(
                        text = "平均获赞",
                        style = MaterialTheme.typography.bodySmall,
                        color = MaterialTheme.colorScheme.outline
                    )
                }
                
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {
                    Text(
                        text = "${userStats.avgCommentsPerPost}",
                        style = MaterialTheme.typography.titleLarge,
                        color = MaterialTheme.colorScheme.primary
                    )
                    Text(
                        text = "平均评论",
                        style = MaterialTheme.typography.bodySmall,
                        color = MaterialTheme.colorScheme.outline
                    )
                }
            }
        }
    }
}

/**
 * 社交互动卡片
 */
@Composable
private fun SocialInteractionCard(userStats: UserStats) {
    Card(
        modifier = Modifier
            .fillMaxWidth()
            .padding(16.dp),
        shape = RoundedCornerShape(16.dp),
        elevation = CardDefaults.cardElevation(defaultElevation = 2.dp),
        colors = CardDefaults.cardColors(
            containerColor = MaterialTheme.colorScheme.surface
        )
    ) {
        Column(
            modifier = Modifier.padding(16.dp)
        ) {
            Text(
                text = "社交互动",
                style = MaterialTheme.typography.titleMedium,
                fontWeight = FontWeight.Bold,
                color = MaterialTheme.colorScheme.primary
            )
            
            Spacer(modifier = Modifier.height(16.dp))
            
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceEvenly
            ) {
                InteractionCard(
                    icon = Icons.Outlined.Group,
                    primaryText = "+${userStats.newFollowers}",
                    secondaryText = "新增粉丝",
                    color = MaterialTheme.colorScheme.primary
                )
                
                InteractionCard(
                    icon = Icons.Outlined.Forum,
                    primaryText = "${userStats.messagesSent}",
                    secondaryText = "消息发送",
                    color = MaterialTheme.colorScheme.secondary
                )
            }
            
            Spacer(modifier = Modifier.height(16.dp))
            
            PieChart(
                data = listOf(
                    PieChartEntry("获赞", userStats.likesReceived, MaterialTheme.colorScheme.primary),
                    PieChartEntry("评论", userStats.commentsReceived, MaterialTheme.colorScheme.tertiary),
                    PieChartEntry("分享", userStats.shareCount, MaterialTheme.colorScheme.secondary)
                )
            )
            
            Spacer(modifier = Modifier.height(16.dp))
            
            // 图表图例
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceEvenly
            ) {
                ChartLegendItem(
                    color = MaterialTheme.colorScheme.primary,
                    label = "获赞: ${userStats.likesReceived}"
                )
                
                ChartLegendItem(
                    color = MaterialTheme.colorScheme.tertiary,
                    label = "评论: ${userStats.commentsReceived}"
                )
                
                ChartLegendItem(
                    color = MaterialTheme.colorScheme.secondary,
                    label = "分享: ${userStats.shareCount}"
                )
            }
        }
    }
}

/**
 * 活动图表
 */
@Composable
private fun ActivityTrendGraph(data: List<DailyActivity>) {
    if (data.isEmpty()) return
    
    val surfaceVariantColor = MaterialTheme.colorScheme.outlineVariant.copy(alpha = 0.3f).toArgb()
    val primaryColor = MaterialTheme.colorScheme.primary.toArgb()
    val tertiaryColor = MaterialTheme.colorScheme.tertiary.toArgb()
    val secondaryColor = MaterialTheme.colorScheme.secondary.toArgb()
    
    Canvas(
        modifier = Modifier
            .fillMaxWidth()
            .height(200.dp)
            .padding(horizontal = 16.dp)
    ) {
        val width = size.width
        val height = size.height
        val usableHeight = height - 40f
        
        // 找出最大活动值以便缩放
        val maxValue = data.maxOfOrNull { max(max(it.posts, it.comments), it.likes) }?.toFloat() ?: 10f
        
        // 绘制背景网格线
        val pathEffect = PathEffect.dashPathEffect(floatArrayOf(10f, 10f), 0f)
        val gridLineColor = surfaceVariantColor
        
        // 水平网格线
        for (i in 0..4) {
            val y = usableHeight - (usableHeight * i / 4) + 20f
            drawLine(
                color = Color(gridLineColor),
                start = Offset(0f, y),
                end = Offset(width, y),
                strokeWidth = 1f,
                pathEffect = pathEffect
            )
        }
        
        // 计算每个点的x坐标
        val xStep = width / (data.size - 1)
        
        // 绘制发布曲线
        drawActivityLine(
            data = data.map { it.posts },
            maxValue = maxValue,
            xStep = xStep,
            height = usableHeight,
            color = primaryColor
        )
        
        // 绘制评论曲线
        drawActivityLine(
            data = data.map { it.comments },
            maxValue = maxValue,
            xStep = xStep,
            height = usableHeight,
            color = tertiaryColor
        )
        
        // 绘制点赞曲线
        drawActivityLine(
            data = data.map { it.likes },
            maxValue = maxValue,
            xStep = xStep,
            height = usableHeight,
            color = secondaryColor
        )
    }
}

/**
 * 在Canvas上绘制活动线
 */
private fun androidx.compose.ui.graphics.drawscope.DrawScope.drawActivityLine(
    data: List<Int>,
    maxValue: Float,
    xStep: Float,
    height: Float,
    color: Int
) {
    for (i in 0 until data.size - 1) {
        val x1 = i * xStep
        val y1 = height - (data[i] / maxValue * height) + 20f
        val x2 = (i + 1) * xStep
        val y2 = height - (data[i + 1] / maxValue * height) + 20f
        
        drawLine(
            color = Color(color),
            start = Offset(x1, y1),
            end = Offset(x2, y2),
            strokeWidth = 3f,
            cap = StrokeCap.Round
        )
    }
    
    // 绘制每个点
    data.forEachIndexed { index, value ->
        val x = index * xStep
        val y = height - (value / maxValue * height) + 20f
        
        drawCircle(
            color = Color(color),
            radius = 4f,
            center = Offset(x, y)
        )
    }
}

/**
 * 绘制饼图
 */
@Composable
private fun PieChart(data: List<PieChartEntry>) {
    val total = data.sumOf { it.value }.toFloat()
    
    if (total <= 0f) return
    
    val surfaceColor = MaterialTheme.colorScheme.surface
    
    Canvas(
        modifier = Modifier
            .size(180.dp)
            .padding(16.dp)
    ) {
        val radius = size.minDimension / 2
        val innerRadius = radius * 0.6f
        val center = Offset(size.width / 2, size.height / 2)
        
        var startAngle = 0f
        
        data.forEach { entry ->
            val sweepAngle = 360f * (entry.value / total)
            
            // 绘制扇形
            drawArc(
                color = entry.color,
                startAngle = startAngle,
                sweepAngle = sweepAngle,
                useCenter = true,
                topLeft = Offset(center.x - radius, center.y - radius),
                size = androidx.compose.ui.geometry.Size(radius * 2, radius * 2)
            )
            
            startAngle += sweepAngle
        }
        
        // 绘制中心空白区域
        drawCircle(
            color = surfaceColor,
            radius = innerRadius,
            center = center
        )
    }
    
    // 图例
    Column {
        data.forEach { entry ->
            Row(
                verticalAlignment = Alignment.CenterVertically,
                modifier = Modifier.padding(vertical = 4.dp)
            ) {
                Box(
                    modifier = Modifier
                        .size(12.dp)
                        .background(entry.color, shape = CircleShape)
                )
                Spacer(modifier = Modifier.width(8.dp))
                Text(
                    text = "${entry.label} (${entry.value})",
                    style = MaterialTheme.typography.bodyMedium
                )
            }
        }
    }
}

/**
 * 饼图数据项
 */
data class PieChartEntry(
    val label: String,
    val value: Int,
    val color: Color
)

/**
 * 统计项目
 */
@Composable
private fun StatsItem(
    icon: ImageVector,
    value: String,
    label: String
) {
    Column(
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Box(
            modifier = Modifier
                .size(48.dp)
                .clip(CircleShape)
                .background(MaterialTheme.colorScheme.primary.copy(alpha = 0.1f)),
            contentAlignment = Alignment.Center
        ) {
            Icon(
                imageVector = icon,
                contentDescription = null,
                tint = MaterialTheme.colorScheme.primary,
                modifier = Modifier.size(24.dp)
            )
        }
        
        Spacer(modifier = Modifier.height(4.dp))
        
        Text(
            text = value,
            style = MaterialTheme.typography.titleMedium,
            fontWeight = FontWeight.Bold
        )
        
        Text(
            text = label,
            style = MaterialTheme.typography.bodySmall,
            color = MaterialTheme.colorScheme.outline
        )
    }
}

/**
 * 高表现帖子项
 */
@Composable
private fun TopPerformingPostItem(
    title: String,
    content: String,
    stats: String
) {
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .clip(RoundedCornerShape(8.dp))
            .background(MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.3f))
            .padding(12.dp)
    ) {
        Text(
            text = title,
            style = MaterialTheme.typography.bodyMedium,
            fontWeight = FontWeight.Bold,
            color = MaterialTheme.colorScheme.primary
        )
        
        Spacer(modifier = Modifier.height(4.dp))
        
        Text(
            text = content,
            style = MaterialTheme.typography.bodyMedium,
            maxLines = 2,
            color = MaterialTheme.colorScheme.onSurface
        )
        
        Spacer(modifier = Modifier.height(4.dp))
        
        Text(
            text = stats,
            style = MaterialTheme.typography.bodySmall,
            color = MaterialTheme.colorScheme.outline
        )
    }
}

/**
 * 互动卡片
 */
@Composable
private fun InteractionCard(
    icon: ImageVector,
    primaryText: String,
    secondaryText: String,
    color: Color
) {
    Card(
        modifier = Modifier
            .padding(4.dp)
            .clip(RoundedCornerShape(12.dp)),
        colors = CardDefaults.cardColors(
            containerColor = color.copy(alpha = 0.1f)
        )
    ) {
        Column(
            horizontalAlignment = Alignment.CenterHorizontally,
            modifier = Modifier.padding(16.dp)
        ) {
            Icon(
                imageVector = icon,
                contentDescription = null,
                tint = color,
                modifier = Modifier.size(32.dp)
            )
            
            Spacer(modifier = Modifier.height(8.dp))
            
            Text(
                text = primaryText,
                style = MaterialTheme.typography.titleLarge,
                fontWeight = FontWeight.Bold,
                color = color
            )
            
            Text(
                text = secondaryText,
                style = MaterialTheme.typography.bodySmall,
                color = MaterialTheme.colorScheme.outline
            )
        }
    }
}

/**
 * 图表图例项
 */
@Composable
private fun ChartLegendItem(
    color: Color,
    label: String
) {
    Row(
        verticalAlignment = Alignment.CenterVertically
    ) {
        Box(
            modifier = Modifier
                .size(12.dp)
                .background(color, shape = CircleShape)
        )
        
        Spacer(modifier = Modifier.width(4.dp))
        
        Text(
            text = label,
            style = MaterialTheme.typography.bodySmall,
            color = MaterialTheme.colorScheme.onSurface
        )
    }
}

/**
 * 时间范围枚举
 */
enum class TimeRange(val displayName: String) {
    WEEK("一周"),
    MONTH("一月"),
    YEAR("一年")
}

/**
 * 每日活动数据类
 */
data class DailyActivity(
    val date: String,
    val posts: Int,
    val comments: Int,
    val likes: Int
)

/**
 * 帖子数据类
 */
data class Post(
    val content: String,
    val likes: Int,
    val comments: Int
)

/**
 * 用户统计数据类
 */
data class UserStats(
    val userId: String,
    val totalPosts: Int,
    val totalComments: Int,
    val likesReceived: Int,
    val commentsReceived: Int,
    val totalFollowers: Int,
    val newFollowers: Int,
    val activeLevel: Int,
    val weeklyActivity: List<DailyActivity>,
    val monthlyActivity: List<DailyActivity>,
    val yearlyActivity: List<DailyActivity>,
    val topPost: Post,
    val mostEngagedPost: Post,
    val avgLikesPerPost: Int,
    val avgCommentsPerPost: Int,
    val messagesSent: Int,
    val shareCount: Int
)

/**
 * 生成示例用户统计数据
 */
private fun generateSampleUserStats(): UserStats {
    val random = Random(System.currentTimeMillis())
    
    // 生成每周活动数据
    val weeklyActivity = List(7) { day ->
        DailyActivity(
            date = "Day ${day + 1}",
            posts = random.nextInt(0, 3),
            comments = random.nextInt(0, 10),
            likes = random.nextInt(0, 20)
        )
    }
    
    // 生成每月活动数据
    val monthlyActivity = List(30) { day ->
        DailyActivity(
            date = "Day ${day + 1}",
            posts = random.nextInt(0, 2),
            comments = random.nextInt(0, 8),
            likes = random.nextInt(0, 15)
        )
    }
    
    // 生成年度活动数据
    val yearlyActivity = List(12) { month ->
        DailyActivity(
            date = "Month ${month + 1}",
            posts = random.nextInt(2, 25),
            comments = random.nextInt(5, 80),
            likes = random.nextInt(20, 150)
        )
    }
    
    val totalPosts = 108
    val totalComments = 324
    
    return UserStats(
        userId = "user_123",
        totalPosts = totalPosts,
        totalComments = totalComments,
        likesReceived = 893,
        commentsReceived = 216,
        totalFollowers = 256,
        newFollowers = 13,
        activeLevel = 8,
        weeklyActivity = weeklyActivity,
        monthlyActivity = monthlyActivity,
        yearlyActivity = yearlyActivity,
        topPost = Post(
            content = "今天参加了马拉松比赛，完成了人生第一个全程马拉松！感谢一路上的加油打气，这是一次突破自我的经历。",
            likes = 126,
            comments = 34
        ),
        mostEngagedPost = Post(
            content = "大家对这个新的功能有什么看法？我觉得还可以再优化一下用户体验。",
            likes = 78,
            comments = 42
        ),
        avgLikesPerPost = 893 / totalPosts,
        avgCommentsPerPost = 216 / totalPosts,
        messagesSent = 156,
        shareCount = 87
    )
} 